/**
 * 定义基于pinia的store存储
 * 设置相关的store数据（combobox）
 * @author Jamen
 * @since 2022-09-22
 */
import { defineStore } from 'pinia';

// 创建store，defineStore调用后返回函数，调用该函数获得Store实体
const useDictionaryStore = defineStore({
  // 必传的参数，id值，在store中唯一
  id: 'useDictionary',
  // 定义数据，返回对象和属性
  state: () => ({
    comboBoxs: [] as Array<ComboBox>, //组合列表
  }),
  // 获取store模块的属性
  getters: {
    getComboBoxs: state => state.comboBoxs,
  },
  // 设置store模块的属性
  actions: {
    //保存comboBoxs
    setComboBoxs(comboBoxs: any) {
      this.comboBoxs = comboBoxs;
    },
    getComboBoxItemName(category: string, value: string) {
      return this.getComboBoxItems(category).find(x => x.value == value).name;
    },
    getComboBoxItems(category: string): Array<ComboBoxItem> {
      const comboBox: ComboBox = this.comboBoxs.find(x => x.category == category);
      return comboBox.comboBoxItems;
    },
  },
});
type ComboBox = {
  category: string;
  comboBoxItems: Array<ComboBoxItem>;
};
type ComboBoxItem = {
  name: string;
  value: string;
};
export default useDictionaryStore;
